<template>
    <div class="">
        <!--顶栏-->
        <header>
            <h1 v-text="webname"></h1>
            <div class="breadcrumb" style="text-align: center;">
                <i class="layui-icon">&#xe715;</i>
                <ul>
                    <li v-for="(vo, i) in address">
                        <a  v-text="vo.name" href="javascript:;" @click="switchPage(vo.url)"></a> <span v-show="i+1 < address.length">/</span>
                    </li>
                </ul>
                <h2 v-show="isTestSystem" style="color: red;"> 测试环境</h2>
            </div>
        </header>
        <div class="main">
            <!--左栏-->
            <div class="left">
                <ul class="cl" >
                    <!--顶级分类-->
                    <li v-for="vo,index in menu" :class="{hidden:vo.hidden, displaynone: !showIt(vo.display)}">
                        <a href="javascript:;"  :class="{active:vo.active, displaynone: !showIt(vo.display)}"  @click="onActive(index)">
                            <i class="layui-icon" v-html="vo.icon"></i>
                            <span v-text="vo.name"></span>
                            <i class="layui-icon arrow" v-show="vo.url.length==0">&#xe61a;</i> <i v-show="vo.active" class="layui-icon active">&#xe623;</i>
                        </a>
                        <!--子级分类-->
                        <div v-for="vo2,index2 in vo.list">
                            <a href="javascript:;" :class="{active:vo2.active, displaynone: !showIt(vo2.display)}" @click="onActive(index,index2)" v-text="vo2.name"></a>
                            <i v-show="vo2.active" class="layui-icon active">&#xe623;</i>
                        </div>
                    </li>
                </ul>
            </div>
            <!--右侧-->
            <div class="right">
                <component :is="currentView" :id="gid"></component>
            </div>
        </div>
    </div>
</template>

<script>
    import Welcome from './Welcome.vue'
    import ClearCache from './ClearCache.vue'
    import Organization from './organization/List.vue'
    import AddOrganization from './organization/Add.vue'
    import EditOrganization from './organization/Edit.vue'
    import Department from './department/List.vue'
    import AddDepartment from './department/Add.vue'
    import EditDepartment from './department/Edit.vue'
    import Customer from './customer/List.vue'
    import AddCustomer from './customer/Add.vue'
    import EditCustomer from './customer/Edit.vue'

    //规格信息
    import SalesSpecification from './salesSpecification/list.vue'
    import addSalesSpecification from './salesSpecification/Add.vue'
    import editSalesSpecification from './salesSpecification/Edit.vue'
    //产品套装信息
    import SuitPack from './suitPack/list.vue'
    import addSuitPack from './suitPack/Add.vue'
    import editSuitPack from './suitPack/Edit.vue'
    //销售权限信息
    import addSalesPermission from './salesPermission/add.vue'
    import SalesPermission from './salesPermission/list.vue'
    import Permissiontransfer from './salesPermission/Permissiontransfer.vue'
    import Usertransfer from './salesPermission/Usertransfer.vue'



    import UserLinkDepaList from './department/UserLinkDepaList.vue'
    import AddUserLinkDepa from './department/AddUserLinkDepa.vue'

    //库房管理
    import Storeroom from './storeroom/list.vue'
    import AddStoreroom from './storeroom/add.vue'
    import EditStoreroom from './storeroom/edit.vue'
    //仓库管理
    import Storehouseroom from './storehouseroom/list.vue'
    import AddStorehouseroom from './storehouseroom/add.vue'
    import EditStorehouseroom from './storehouseroom/edit.vue'

    //库房管理F
    import StoreroomF from './storeroomF/list.vue'
    import AddStoreroomF from './storeroomF/add.vue'
    import EditStoreroomF from './storeroomF/edit.vue'
    //仓库管理F
    import StorehouseroomF from './storehouseroomF/list.vue'
    import AddStorehouseroomF from './storehouseroomF/add.vue'
    import EditStorehouseroomF from './storehouseroomF/edit.vue'

    //导出
    import Exporywhoutform from './whoutform/Export.vue'
    import Exporywhinform from './whinform/Export.vue'
    import Exporypurchaseorder from './purchaseorder/Export.vue'
    import Transferapply from './transferapply/Export.vue'
    import Exporysalesorder from './salesorder/Export.vue'
    import Exporysalesreport from './salesreport/Export.vue'
    import ViewSellform from './salesreport/ViewSellform.vue'
    import Exporywhinfo from './whinfo/Export.vue'
    import RptStoreAnalysis from './rpt_storeanalysis/Export.vue'
    import ProduceReport from './producereport/Export.vue'


    //生产加工-数据类别
    import ListDatacategory from './datacategory/List.vue'
    import AddDatacategory from './datacategory/Add.vue'
    import EditDatacategory from './datacategory/Edit.vue'

    //生产加工-数据项
    import ListDataItem from './dataItem/List.vue'
    import AddDataItem from './dataItem/Add.vue'
    import EditDataItem from './dataItem/Edit.vue'

    //生产加工-标准表
    import ListStandard from './standard/List.vue'
    import AddStandard from './standard/Add.vue'
    import EditStandard from './standard/Edit.vue'

    import Productiondaily from './productiondaily/Export.vue'
    import ProductiondailyList from './productiondaily/list.vue'

    //质检项
    import Qaitems from './qaitems/List.vue'
    import QaitemsAdd from './qaitems/Add.vue'
    import QaitemsEdit from './qaitems/Edit.vue'

    //质检标准管理
    import Qamain from './qamain/List.vue'
    import QamainAdd from './qamain/Add.vue'
    import QamainEdit from './qamain/Edit.vue'

    //质检项配置表管理
    import Qaassignment from './qaassignment/List.vue'
    import QaassignmentAdd from './qaassignment/Add.vue'
    import QaassignmentEdit from './qaassignment/Edit.vue'

    //采收系统--账单管理
    import PayinfoList from './harvest/payinfoList.vue'
    import FreightorderList from './harvest/freightorderList.vue'
    //采收系统--付款申请
    import PaymentApplication from './harvest/PaymentApplication.vue'
    import FreightorderPayList from './harvest/freightorderPayList.vue'
    //采收系统--付款申请
    import FreightorderExport from './harvest/freightorderExport.vue'
    //货运单详情
    import FreightorderForder from './harvest/FreightorderForder'
    //采收批次
    import BatchList from './harvest/batch/List.vue'
    import EditBatch from './harvest/batch/Edit.vue'
    import AddBatch from './harvest/batch/Add.vue'
    //采收合同
    import ContractList from './harvest/contract/List.vue'
    import AddContract from './harvest/contract/Add.vue'
    import EditContract from './harvest/contract/Edit.vue'

    //产品价格
    import productprice from './productprice/list.vue'
    import addproductprice from './productprice/Add.vue'
    import editproductprice from './productprice/Edit.vue'



    import AssignList from './customer/AssignList.vue'
    import AssignAdd from './customer/AssignAdd.vue'

    //客户管理
    import CustomerList from './customer/List.vue'
    import CustomerAdd from './customer/Add.vue'
    import CustomerEdit from './customer/Edit.vue'
    import CustomerDetails from './customer/Details.vue'
    import CustomerRecbalance from './customer/Recbalance.vue'


    //物流管理
    import WlcompanyList from './wlcompany/List.vue'
    import WlcompanyAdd from './wlcompany/Add.vue'
    import WlcompanyEdit from './wlcompany/Edit.vue'
    import WlinfoList from './wlcompany/WlinfoList.vue'
    import WlExpensesPay from './wlcompany/PayExpenses.vue'
    import WlReExpensesPay from './wlcompany/RePayExpenses.vue'
    import WlPayExpensesApply from './wlcompany/PayExpensesApply.vue' //运费支付申请
    import WlPayExpensesList from './wlcompany/PayExpensesList.vue'
    import WlViewPayExpenses from './wlcompany/ViewPayExpenses.vue'
    import PassportExamine from './wlcompany/PassportExamine.vue' //运费支付主管审批
    import WlExtras from './wlcompany/WlExtras.vue' //附设属性
    import WlExtrasAdd from './wlcompany/WAdd.vue'  //附设属性添加

    import WlExtrasEAdd from './wlcompany/EAdd.vue' //附属箱添加
    import WlExtrasEEdit from './wlcompany/EEdit.vue'//附属箱修改
    import WlExtrasWEdit from './wlcompany/WEdit.vue'//附属w修改

    //渠道管理
    import ChannelAdd from './channel/Add.vue'
    import ChannelList from './channel/List.vue'
    import ChannelEdit from './channel/Edit.vue'

     import SalesPlan from './saleplan/List.vue'
     import RechargeReport from './recharge/List.vue'
     import AddSalePlan from './saleplan/Add.vue'
     import EditSalePlan from './saleplan/Edit.vue'

      //调度日报表
    import DailyDispatchReport from './dailyDispatchReport/List'
    //每日库存重跑
    import Inventory  from './Inventory/index'
    //每日动态库存重跑
    import Inventory1  from './Inventory/index1'

    //种植管理
    //种植批次
    import Farmerbatch from './farmerbatch/list.vue'
    import AddFarmerbatch from './farmerbatch/add.vue'
    import EditFarmerbatch from './farmerbatch/edit.vue'
    //种植户管理
    import FarmerInfo from './farmerinfo/list.vue'
    import AddFarmerInfo from './farmerinfo/add.vue'
    import EditFarmerInfo from './farmerinfo/edit.vue'
    //订单管理
    import FarmerOrder from './farmerorder/list.vue'
    import AddFarmerOrder from './farmerorder/add.vue'
    import EditFarmerOrder from './farmerorder/edit.vue'
    //可交换产品管理
    import InterchangeSpec from './interchangespec/list.vue'
    import AddInterchangeSpec from './interchangespec/add.vue'
    import EditInterchangeSpec from './interchangespec/edit.vue'
    //田检标准
    import PlantCheck from './plantcheck/list.vue'
    import AddPlantCheck from './plantcheck/add.vue'
    import EditPlantCheck from './plantcheck/edit.vue'
    //田检相
    import PlantCheckitems from './plantcheckitems/list.vue'
    import AddPlantCheckitems from './plantcheckitems/add.vue'
    import EditPlantCheckitems from './plantcheckitems/edit.vue'
    //田检记录
    import PlantCheckOperation from './plantcheckoperation/list.vue'
    import ExportPlantCheckOperation from './plantcheckoperation/export.vue'
    //农事记录
    import PlantOperation from './plantoperation/list.vue'
    import ExportPlantOperation from './plantoperation/export.vue'

    //报表统计（农户）
    import ExporywhoutformF from './whoutform/ExportF.vue'
    import ExporywhinformF from './whinform/ExportF.vue'
    import ExporywhinfoF from './whinfo/ExportF.vue'
    import RptStoreAnalysisF from './rpt_storeanalysis/ExportF.vue'

    export default {
        name: 'Entry',
        data () {
            return {
                menu:[],
                address:[{
                    name:'首页',
                    url:'Welcome'
                }],
                currentView: 'Welcome',
                refView: 'Welcome',
                gid: '',
                permission: JSON.parse(sessionStorage.getItem('permissions')),
                isTestSystem: location.hostname.indexOf('test') > -1
            }
        },
        components: {
            Welcome,
            ClearCache,
            Organization,
            AddOrganization,
            EditOrganization,
            Department,
            AddDepartment,
            EditDepartment,
            AssignList,
            AssignAdd,
            CustomerList,
            CustomerAdd,
            CustomerEdit,
            CustomerDetails,
            CustomerRecbalance,

            //物流管理
            WlcompanyList,
            WlcompanyAdd,
            WlcompanyEdit,
            WlinfoList,
            WlExpensesPay,
            WlReExpensesPay,
            WlPayExpensesApply,
            WlPayExpensesList,
            WlViewPayExpenses,
            PassportExamine,
            WlExtras,
            WlExtrasAdd,
            WlExtrasEAdd,
            WlExtrasEEdit,
            WlExtrasWEdit,

            //规格信息
            SalesSpecification,
            addSalesSpecification,
            editSalesSpecification,

            //规格信息
            SuitPack,
            addSuitPack,
            editSuitPack,

            //销售权限信息
            addSalesPermission,
            SalesPermission,
            Permissiontransfer,
            Usertransfer,

            //库房管理
            Storeroom,
            AddStoreroom,
            EditStoreroom,
            //仓库管理
            Storehouseroom,
            AddStorehouseroom,
            EditStorehouseroom,

            //用户所属部门
            UserLinkDepaList,
            AddUserLinkDepa,

            //出库单导出
            Exporywhoutform,
            Exporywhinform,
            Exporypurchaseorder,
            Transferapply,
            Exporysalesorder,
            Exporysalesreport,
            Exporywhinfo,
            RptStoreAnalysis,

            //销售单详情
            ViewSellform,

            //生产加工-数据类别
            ListDatacategory,
            AddDatacategory,
            EditDatacategory,

            //生产加工-数据项
            ListDataItem,
            AddDataItem,
            EditDataItem,

            //生产加工-标准表
             ListStandard  ,
             AddStandard  ,
             EditStandard,
            Productiondaily,
            ProductiondailyList,

            //质检项
            Qaitems,
            QaitemsAdd,
            QaitemsEdit,

            //质检标准管理
            Qamain,
            QamainAdd,
            QamainEdit,

            //质检项配置
            Qaassignment,
            QaassignmentAdd,
            QaassignmentEdit,

            //采收系统-账单管理
            PayinfoList,
            FreightorderList,
            //采收系统--付款申请
            PaymentApplication,
            FreightorderPayList,
            //采收系统--货运单导出
            FreightorderExport,
            FreightorderForder,
            //批次管理
            BatchList,
            EditBatch,
            AddBatch,
            //合同管理
            ContractList,
            AddContract,
            EditContract,

            //规格信息
            productprice,
            addproductprice,
            editproductprice,

            //渠道管理
            ChannelAdd,
            ChannelList,
            ChannelEdit,

            //生产报表
            ProduceReport,

            //销售计划
            SalesPlan,
            RechargeReport,
            AddSalePlan,
            EditSalePlan,
               //调度日报表
            DailyDispatchReport,
            //每日库存重跑
           Inventory,
            //每日动态库存重跑
           Inventory1,

            //种植管理
            //种植批次
            Farmerbatch,
            AddFarmerbatch,
            EditFarmerbatch,
            //种植户管理
            FarmerInfo,
            AddFarmerInfo,
            EditFarmerInfo,
            //订单管理
            FarmerOrder,
            AddFarmerOrder,
            EditFarmerOrder,
            //可交换产品管理
            InterchangeSpec,
            AddInterchangeSpec,
            EditInterchangeSpec,
            //田检标准
            PlantCheck,
            AddPlantCheck,
            EditPlantCheck,
            //田检相
            PlantCheckitems,
            AddPlantCheckitems,
            EditPlantCheckitems,
            //田检记录
            PlantCheckOperation,
            ExportPlantCheckOperation,
            //农事记录
            PlantOperation,
            ExportPlantOperation,

            //报表统计（农户）
            //库房管理
            StoreroomF,
            AddStoreroomF,
            EditStoreroomF,
            //仓库管理
            StorehouseroomF,
            AddStorehouseroomF,
            EditStorehouseroomF,

            ExporywhoutformF,
            ExporywhinformF,
            ExporywhinfoF,
            RptStoreAnalysisF
        },
        created() {
            let that = this;
            //加载左侧菜单
            let url = '/api/menu';
            this.$get(url).then(response => {
                this.menu = response.data;
                /*SalesPermission
                * 2020-04-01
                * 修改菜单权限控制
                * lrj
                */

                //all 为超级管理员，可以看到所有菜单
               if(this.permission != null && this.permission.type != 'All'){
                   this.menu.map(function (item) {
                       //非超级管理员
                       if (item.funcType.length != 0) {
                           item.list.map(function (page) {
                               if (page.funcType != 0) {
                                   page.display = that.permission.functypes.indexOf(page.funcType.toString()) > -1;
                               } else {
                                   page.display = that.compareArr(that.permission.permis, page.permis);
                               }
                           })
                           item.display = that.compareArr(that.permission.functypes, item.funcType);
                       }

                       return item;
                   })
               }
                that.thisActive(0, false);
                that.thisAttr();
            })
        },
        mounted() {
            //首页登录判断
            this.checkLogin();

            if (this.$route.query.pid != undefined && this.$route.query.component != undefined) {
                this.gid = this.$route.query.pid;
                this.currentView = this.$route.query.component;
            }
        },
        watch: {
            'currentView': function (newview, oldview) {
                this.refView = oldview;
            }
        },
        methods: {
            switchPage(view) {
                this.checkLogin();
                this.currentView = view;
                console.log('permission:', this.permission)
            },
            toLogin() {
                let that = this;
                layui.use(['form'], function () {
                    var layer = layui.layer;
                    layer.confirm("确定要退出系统？", function (index) {
                        sessionStorage.removeItem('user');
                        that.$router.push({name: 'Login'});
                        layer.close(index);
                    })
                })
            },
            //记住收展
            onActive:function (pid,id=false) {
                //先整体关闭菜单
                this.menu.forEach((v,k)=>{
                    v.hidden = true;
                    v.active = false;
                    v.list.forEach((v2,k2)=>{
                        v2.active = false;
                    })
                })

                let data;
                if(id===false){

                    data = this.menu[pid];

                    if(data.url.length>0){
                        this.menu.forEach((v,k)=>{
                            v.active = false;
                            v.list.forEach((v2,k2)=>{
                                v2.active = false;
                            })
                        })

                        data.active = true;

                    }

                    data.hidden = !data.hidden;

                }else{
                    data = this.menu[pid].list[id];
                }
                this.updateStorage();
                if(data.url.length>0){
                    if (data.url == 'Logout') {
                        this.toLogin();
                    } else {
                        this.switchPage(data.url);
                    }
                }

                //选中状态赋值
                this.thisActive(pid, id);
                this.thisAttr();
            },

            //更新菜单缓存
            updateStorage(){
                sessionStorage.menu = JSON.stringify(this.menu);
            },
            //菜单高亮
            thisActive:function(pid,id){
                this.menu[pid].hidden = false;
                if(id!==false){
                    this.menu[pid].list[id].active = true;
                }else{
                    if(pid!==false){
                        this.menu[pid].active = true;
                    }
                }

                this.updateStorage();

            },
            //当前位置
            thisAttr:function () {
                //当前位置
                let address = [];
                this.menu.forEach((v,k)=>{
                    if (v.url == 'Welcome' && v.active) {
                        address.push({
                            name:v.name,
                            url:v.url
                        })
                        this.address = address;
                    } else {
                        v.list.forEach((v2,k2)=>{
                            if(v2.active){
                                address.push({
                                    name:v.name,
                                    url:'javascript:;'
                                })
                                address.push({
                                    name:v2.name,
                                    url:v2.url,
                                })
                                this.address = address;
                            }
                        })
                    }
                })
            },
            //公共方法
            //serializeArray 字符串转json字符串
            toJsonStr: function (arrData) {
                let obj = {};
                for (let i=0; i<arrData.length; i++) {
                    obj[arrData[i].name] = arrData[i]['value'];
                }
                return JSON.stringify(obj);
            },
            back: function () {
                this.switchPage(this.refView);
            },
            //对比两个数组是否有相同的元素
            compareArr: function(arr1, arr2) {
                let exists = false;
                if(arr1 instanceof Array && arr2 instanceof Array)
                {
                    for (var i=0,iLen=arr1.length; i<iLen; i++)
                    {
                        for (var j=0,jLen=arr2.length; j<jLen; j++)
                        {
                            if (arr1[i]==arr2[j])
                            {
                                return true;
                            }
                        }
                    }
                }
                return exists;
            },
            showIt: function(showFlag) {
                return this.permission != null && this.permission.type == 'All' ? true : showFlag;
            },
        }
    }
</script>

<style scoped>
    .layui-form-switch {
        min-width: 50px !important;
    }
    .displaynone {
        display: none;
    }
</style>
